package Manager

import (
	"backend/Remote"
	"fmt"
	"path/filepath"
	"strings"
	"time"
)

func RsyncLocalFold(localDir string, isFully bool) {
	localFiles := getLocalFiles(localDir)

	if isFully {
		getRemoteMD5sum()
	} else {
		getRemoteMD5sumByLocalFileName(localFiles)
		//fmt.Println("rsync.go->RsyncLocalFold->getRemoteMD5sumByLocalFileName", localFiles)
	}
	rsyncToHosts(localDir)
	//fmt.Println("Host.go->RsyncLocalFold->rsyncToHosts")
	printOut(localDir)
}

func printOut(localFolder string) {
	for i := range Remote.Hosts {
		fmt.Println()
		host := &Remote.Hosts[i]
		if len(host.ToReceiveFiles) > 0 || len(host.ToDeleteFiles) > 0 {
			fmt.Printf("执行结果：\n【%s】服务器\n", host.IP)
		}
		for _, f := range host.ToDeleteFiles {
			Path := filepath.Join(host.Dir, f)
			linuxPath := strings.ReplaceAll(Path, `\`, `/`)
			fmt.Printf("删除文件：%s\n", linuxPath)
		}
		fmt.Println()
		for _, f := range host.ToReceiveFiles {
			Path := filepath.Join(localFolder, f)
			fmt.Printf("上传文件：%s\n", Path)
		}
	}
	time.Sleep(time.Second)
}
